<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/11/21 Time: 14:46
 */

namespace App\Http\Controllers\Mobile;


use App\Http\Controllers\Controller;
use App\Http\Request\Order\CartStore;
use App\Repositories\Order\CartRepository;
use App\Traits\ApiRespond;
use Illuminate\Http\Request;

class CartController extends Controller
{
    use ApiRespond;

    private $cart;

    public function __construct(CartRepository $cart)
    {
        $this->cart = $cart;
    }

    public function carts()
    {
        $carts = $this->cart->carts();
        return $this->respondWith($carts);
    }

    /**
     *
     * 商品类型总数
     * author Paco
     * createTime 2018/11/21 15:29
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function count()
    {
        $count = $this->cart->count();
        return $this->respondWith($count);
    }


    /**
     *
     * 商口总数数量
     * author Paco
     * createTime 2018/11/21 15:29
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function totalCount()
    {
        $count = $this->cart->totalCount();
        return $this->respondWith($count);
    }

    /**
     *
     * save
     * author Paco
     * createTime 2018/11/21 15:18
     * @param CartStore $cart
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function save(CartStore $cart, Request $request)
    {
        $params = $cart->validated($request);
        $this->cart->saveItem($params['product_id'], $params['sku_id'], $params['quantity']);
        return $this->respondSuccess();
    }

    /**
     *
     * increment
     * author Paco
     * createTime 2018/11/21 15:26
     * @param CartStore $cart
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function increment(CartStore $cart, Request $request)
    {
        $params = $cart->validated($request);
        $this->cart->increment($params['product_id'], $params['sku_id']);
        return $this->respondSuccess();
    }

    /**
     *
     * decrement
     * author Paco
     * createTime 2018/11/21 15:24
     * @param CartStore $cart
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function decrement(CartStore $cart, Request $request)
    {
        $params = $cart->validated($request);
        $this->cart->decrement($params['product_id'], $params['sku_id']);
        return $this->respondSuccess();
    }

    /**
     *
     * delItem
     * author Paco
     * createTime 2018/11/21 15:42
     * @param CartStore $cart
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function delItem(CartStore $cart, Request $request)
    {
        $params = $cart->validated($request);
        $this->cart->delItem($params['product_id'], $params['sku_id']);
        return $this->respondSuccess();
    }


}